import "package:form_builder_validators/form_builder_validators.dart";

abstract final class FormValidator {
  static String? Function(String?) email() =>
      FormBuilderValidators.email(errorText: "请输入正确格式的邮箱");

  static String? Function(String?) equalFieldValue(
    String? Function() getFieldValue,
    errorText,
  ) {
    return (value) {
      if (value != getFieldValue()) return errorText;
      return null;
    };
  }

  static String? Function(String?) password() => FormBuilderValidators.match(
        r"^(?![a-zA-Z]+$)(?!\d+$)(?![^\da-zA-Z\s]+$).{8,20}$",
        errorText: "请输入正确格式的密码",
      );

  static String? Function(String?) maxLength(int maxLength) =>
      FormBuilderValidators.maxLength(maxLength, errorText: "最多$maxLength字符");

  static String? Function(String?) minLength(int minLength, String errorText) {
    return FormBuilderValidators.minLength(minLength, errorText: errorText);
  }

  static String? Function(T?) required<T>(String errorText) =>
      FormBuilderValidators.required(errorText: errorText);

  static String? Function(String?) phone(String phone) => FormBuilderValidators.match(
      r"^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}$",
      errorText: "请输入正确的手机号码",
  );
}
